﻿using AnesSystem.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Controls
{
    /// <summary>
    /// MyDatePickerCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class MyDatePickerCtrl : UserControl
    {
        #region
        private MyDatePickerCtrlViewModel _myDatePickerCtrlViewModel=new MyDatePickerCtrlViewModel();
        public MyDatePickerCtrl()
        {
            InitializeComponent();
            //this.initParameters();
            this._minute.Background = Brushes.White;
            this._second.Background = Brushes.White;
            this._hour.Background = Brushes.White;
            this._grid.DataContext = MyDatePickerCtrlViewModel;
        }
        
        public DateTime SelectedDateTime
        {
            get { return (DateTime)GetValue(SelectedDateTimeProperty); }
            set { SetValue(SelectedDateTimeProperty, value); }
        }

        public MyDatePickerCtrlViewModel MyDatePickerCtrlViewModel
        {
            get
            {
                return _myDatePickerCtrlViewModel;
            }

            set
            {
                _myDatePickerCtrlViewModel = value;
            }
        }

        // Using a DependencyProperty as the backing store for DateTimeProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedDateTimeProperty =
            DependencyProperty.Register("SelectedDateTime", typeof(DateTime), typeof(MyDatePickerCtrl), new PropertyMetadata(DateTime.Now));
        #endregion

        #region 业务处理函数
        /// <summary>
        /// 更改选中状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textbox_hour_SelectionChanged(object sender, RoutedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
            {
                switch (tb.Name)
                {
                    case "_hour":
                        tb.Background = Brushes.Gray;
                        this._minute.Background = Brushes.White;
                        this._second.Background = Brushes.White;
                        break;
                    case "_minute":
                        tb.Background = Brushes.Gray;
                        this._hour.Background = Brushes.White;
                        this._second.Background = Brushes.White;
                        break;
                    case "_second":
                        tb.Background = Brushes.Gray;
                        this._hour.Background = Brushes.White;
                        this._minute.Background = Brushes.White;
                        break;
                }
            }
        }



        /// <summary>
        /// 向上加时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Up_Click(object sender, RoutedEventArgs e)
        {
            if (this._hour.Background == Brushes.Gray)
                MyDatePickerCtrlViewModel.Up_Click("_hour");
            else if (this._minute.Background == Brushes.Gray)
                MyDatePickerCtrlViewModel.Up_Click("_minute");
            else if (this._second.Background == Brushes.Gray)
                MyDatePickerCtrlViewModel.Up_Click("_second");
            SelectedDateTime = MyDatePickerCtrlViewModel.Dat;
        }

        /// <summary>
        /// 向下减时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Down_Click(object sender, RoutedEventArgs e)
        {
            if (this._hour.Background == Brushes.Gray)
                MyDatePickerCtrlViewModel.Down_Click("_hour");
            else if (this._minute.Background == Brushes.Gray)
                MyDatePickerCtrlViewModel.Down_Click("_minute");
            else if (this._second.Background == Brushes.Gray)
                MyDatePickerCtrlViewModel.Down_Click("_second");
            SelectedDateTime = MyDatePickerCtrlViewModel.Dat;
        }




        ///// <summary>
        ///// 初始化参数设置
        ///// </summary>
        //private void initParameters()
        //{
        //    string strt = System.DateTime.Now.ToString("HH:mm:ss");
        //    this._hour.Text = strt.Split(':')[0];
        //    this._minute.Text = strt.Split(':')[1];
        //    this._second.Text = strt.Split(':')[2];
        //}

        ///// <summary>
        ///// 数字标准化处理
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void numtextboxchanged(object sender, TextChangedEventArgs e)
        //{
        //    TextBox tb = sender as TextBox;
        //    if (tb != null)
        //    {
        //        if ((this.isNum(tb.Text) == false) || (tb.Text.Length > 2))
        //        {
        //            tb.Text = "0";
        //            return;
        //        }
        //    }
        //}

        ///// <summary>
        ///// 判断是否为数字，是--->true，否--->false
        ///// </summary>
        ///// <param name="str"></param>
        ///// <returns></returns>
        //private bool isNum(string str)
        //{
        //    bool ret = true;
        //    foreach (char c in str)
        //    {
        //        if ((c < 48) || (c > 57))
        //        {
        //            return false;
        //        }
        //    }

        //    return ret;
        //}

        #endregion

    }


}
